package mongo

import (
	"sync/atomic"

	"engine/log"
)

var _gMongoCli *cli
var _gMongoInstCount atomic.Int64

func Start() (c Cli, err error) {
	if n := _gMongoInstCount.Add(1); n == 1 {
		_gMongoCli = &cli{}
		log.GetLogger().Info("start mongo").Println()
		err = _gMongoCli.start()
	}
	return _gMongoCli, err
}

func Stop() {
	old := _gMongoCli
	if n := _gMongoInstCount.Add(-1); n == 0 {
		old.stop()
		log.GetLogger().Info("stop mongo").Println()
	}
}

func GetInstance() Cli {
	return _gMongoCli
}
